﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace EcodexIntegrationSample.Controllers
{
    class frmGetStatusTimbradoController
    {
        private View.IfrmGetStatusTimbrado _view;
        private Random _random;

        public frmGetStatusTimbradoController(View.IfrmGetStatusTimbrado view)
        {
            _view = view;
            _view.onGetStatusTimbrado += _view_onGetStatusTimbrado;
            _random = new Random();
        }

        void _view_onGetStatusTimbrado(object sender, EventArgs e)
        {
            String rfc = _view.RFC;
            String UUID = _view.UUID;
            Int64 transactionNueva = _random.Next();
            Int64 transactionID = _view.transactionID;
            string token = string.Empty;
            string Error;
            Utils.Utilerias Util = new Utils.Utilerias(_view);
            Error = Util.CreateToken(rfc, transactionID, ref token);
            if (!string.IsNullOrEmpty(Error))
            {
                _view.ShowUnknownError(Error);
                return;
            }
            try
            {
                #region Status Timbrado
                using (var srvTimbrado = new SrvTimbrado.TimbradoClient())
                {
                    var respuesta = srvTimbrado.EstatusTimbrado(rfc, token, ref transactionNueva, transactionID, UUID);
                    _view.code = respuesta.Codigo;
                    _view.description = respuesta.Descripcion;
                }
                #endregion
            }
            #region Security service exceptions
            catch (FaultException<SrvSecurity.FallaServicio> serviceFault)
            {
                using (var errorForm = new Forms.Error(TranslateFault.ToServiceError(serviceFault.Detail)))
                {
                    errorForm.ShowDialog();
                }
            }
            catch (FaultException<SrvSecurity.FallaSesion> sessionFauld)
            {
                using (var errorForm = new Forms.Error(TranslateFault.ToSessionError(sessionFauld.Detail)))
                {
                    errorForm.ShowDialog();
                }
            }
            #endregion
            #region Timbrado service exceptions
            catch (FaultException<SrvTimbrado.FallaServicio> serviceFault)
            {
                using (var errorForm = new Forms.Error(TranslateFault.ToServiceError(serviceFault.Detail)))
                {
                    errorForm.ShowDialog();
                }
            }
            catch (FaultException<SrvTimbrado.FallaSesion> sessionFauld)
            {
                using (var errorForm = new Forms.Error(TranslateFault.ToSessionError(sessionFauld.Detail)))
                {
                    errorForm.ShowDialog();
                }
            }
            #endregion
            catch (TimeoutException)
            {
                _view.ShowUnknownError("Error de tiempo de espera del servicio");
            }
            catch (Exception ex)
            {
                _view.ShowUnknownError(ex.Message);
            }
        }
    }
}
